#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include <math.h>
#include "Obstacle.h"

namespace SheepDefence 
{
	Obstacle::Obstacle()
	{
	}

	Obstacle::~Obstacle()
	{
		for (unsigned int i = 0; i < lines.size(); i++)
			delete lines[i];

		for (unsigned int i = 0; i < circles.size(); i++)
			delete circles[i];
	}

	Obstacle::Obstacle(SypX::FileInputStream& fin, SypX::Sprite* sprite)
	{
		SypX::String temp;

		SypX::ErrorLogger::getLogger() << "Loading Obstacle...\n";

		this->sprite = sprite;
		
		std::getline(fin, temp);	// Absorb name

		fin >> center_x;
		fin >> center_y;
		fin >> circleCount;

		for (int i = 0; i < circleCount; i++)
		{
			circle *col = new circle();
			
			fin >> col->x[0];
			fin >> col->y[0];
			fin >> col->r; 

			for (int j = 1; j < 12; j++)
			{
				col->x[j] = (float) (cos(3.1415926535897932384626433832795 * -j / 6) * col->x[0] - sin(3.1415926535897932384626433832795 * -j / 6) * col->y[0]);
                col->y[j] = (float) (sin(3.1415926535897932384626433832795 * -j / 6) * col->x[0] + cos(3.1415926535897932384626433832795 * -j / 6) * col->y[0]);
			}

			circles.push_back(col);
		}

		fin >> lineCount;

		for (int i = 0; i < lineCount; i++)
		{
			line *col = new line();

			fin >> col->x1[0];
			fin >> col->y1[0];
			fin >> col->x2[0];
			fin >> col->y2[0];

			for (int j = 1; j < 12; j++)
			{
				col->x1[j] = (float) (cos(3.1415926535897932384626433832795 * -j / 6) * col->x1[0] - sin(3.1415926535897932384626433832795 * -j / 6) * col->y1[0]);
                col->y1[j] = (float) (sin(3.1415926535897932384626433832795 * -j / 6) * col->x1[0] + cos(3.1415926535897932384626433832795 * -j / 6) * col->y1[0]);
				col->x2[j] = (float) (cos(3.1415926535897932384626433832795 * -j / 6) * col->x2[0] - sin(3.1415926535897932384626433832795 * -j / 6) * col->y2[0]);
                col->y2[j] = (float) (sin(3.1415926535897932384626433832795 * -j / 6) * col->x2[0] + cos(3.1415926535897932384626433832795 * -j / 6) * col->y2[0]);
			}

			lines.push_back(col);
		}

		speed = 0;
		
		SypX::ErrorLogger::getLogger() << "Loaded: " << speed << "\n";

		type = OBSTACLE;
	}
}